import { message } from "ant-design-vue";
import { useRouter } from "vue-router";
import storage from "@/utils/storage";
// 响应成功
const responseSuccess = (response: Record<string, any>) => {
 
  if (response && (response.status === 200 || response.status === 304)) {
    if (response.data && response.data.retCode === 0) {
      return response.data;
    } else {
      message.error(response.data.retMsg);
      return;
    }
  } else {
    // 异常状态下，把错误信息返回去
    if (response && response.data && response.data.retMsg) {
      message.error(response.data.retMsg);
      return;
    } else if (response && response.status === 404) {
      message.error("访问异常");
      return;
    } else {
      message.error("网络错误");
      return;
    }
  }
}
// 响应错误
const responseError = (error: Record<string, any>) => {
  const { status } = error.response || {};
  const router = useRouter();
  const { data: { retMsg = "" } = { retMsg: "" } } = error.response || {};
  if (status === 401) {
    storage.removeAll();
    router.push({ name: "login" });
  }

  if (retMsg) {
    message.error(retMsg);
  }

  return Promise.reject(error);
}

export {
  responseSuccess,
  responseError
}